package com.example.test0.Controller;

import com.example.test0.mapper.UserMapper;
import com.example.test0.model.User;
import com.google.gson.Gson;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.lang.*;

import java.util.List;

@Controller
public class TestController {
    @Autowired
    private UserMapper userMapper;


    @GetMapping("/testTest")
    @ResponseBody
    public Integer testTest(Integer num) {
        Integer ret = 100;
        if (num == null)
            ret = 10;
        else if (num == 1) num = 2;
        else if (num == 2) num = 1;
        ret = num;
        System.out.println(ret);
        return ret;
    }

    @PostMapping("/test1")
    @ResponseBody
    public String testSelect1(HttpServletRequest request) {

        HttpSession session = request.getSession();//这就是session的创建
        session.setAttribute("user", "visitor");//给session添加属性属性name： user,属性 value：visitor
        session.setAttribute("username", "tom");//添加属性 name: username; value: tom

        String ret = null;
        System.out.println(("----- selectAll method test ------"));
        List<User> userList = userMapper.selectList(null);
        System.out.println("**************************************************");
        System.out.println("test1");
        userList.forEach(System.out::println);
        User user = userList.get(0);
        System.out.println("**************************************************");
        ret = new Gson().toJson(userList);
        System.out.println(ret);
        //ret = userList.toString();
        return ret;
        //return userList.toString();
    }

    @GetMapping("/test1")
    public String testSelect11() {
        return "test1";
    }

    @PostMapping("/test2")
    @ResponseBody
    public String testSelect2(HttpServletRequest request) {


        String ret = null;
        System.out.println(("----- selectAll method test ------"));
        List<User> userList = userMapper.findAllUser();
        List<User> userList0 = userMapper.selectList(null);
        System.out.println("**************************************************");
        System.out.println("test2");
        userList.forEach(System.out::println);
        System.out.println(userList0);
        System.out.println("**************************************************");
        ret = new Gson().toJson(userList);
        return ret;
    }

//    @PostMapping("/test3")
//    @ResponseBody
//    public String testSelect3(User user){
//        String ret = null;
//        System.out.println(("----- selectAll method test ------"));
//        User myuser= userMapper.findByUser(user);
//        System.out.println("**************************************************");
//        System.out.println("test3");
//        System.out.println(myuser);
//        System.out.println("**************************************************");
//        ret = new Gson().toJson(myuser);
//        return ret;
//    }

    @PostMapping("/test3")
    @ResponseBody
    public String testSelect3(User user) {
        String ret = null;
        System.out.println(("----- selectAll method test ------"));
        User myuser = userMapper.findByUser(user);
        System.out.println("**************************************************");
        System.out.println("test3");
        System.out.println(myuser);
        System.out.println("**************************************************");
        ret = new Gson().toJson(myuser);
        return ret;
    }

    @PostMapping("/test4")
    public String testSelect4(User user) {
        String ret = null;
        System.out.println(("----- selectAll method test ------"));
        User myuser = userMapper.findById(user);
        System.out.println("**************************************************");
        System.out.println("test4");
        System.out.println(myuser);
        System.out.println("**************************************************");
        ret = new Gson().toJson(myuser);
        return ret;
    }

    @PostMapping("/test5")
    public String testSelect5() {
        String ret = null;
        System.out.println(("----- selectAll method test ------"));
        List<User> userList = userMapper.findAll();
        System.out.println("**************************************************");
        System.out.println("test5");
        System.out.println(userList);
        System.out.println("**************************************************");
        ret = new Gson().toJson(userList);
        return ret;
    }

    @PostMapping("/test6")
    public String testSelect6() {
        String ret = null;
        System.out.println(("----- selectAll method test ------"));
        List<User> userList = userMapper.findAllTest();
        System.out.println("**************************************************");
        System.out.println("test6");
        System.out.println(userList);
        System.out.println("**************************************************");
        ret = new Gson().toJson(userList);
        return ret;
    }

    @PostMapping("/test7")
    public String testSelect7(User user) {
        String ret = null;
        System.out.println(("----- selectAll method test ------"));
        List<User> userList = userMapper.findAllTest0(user);
        System.out.println("**************************************************");
        System.out.println("test7");
        System.out.println(userList);
        System.out.println("**************************************************");
        ret = new Gson().toJson(userList);
        return ret;
    }

    @GetMapping("/test8")
    public int testReturn(int num) {
        if (num == 1)
            num = 2;
        if (num == 2)
            num = 1;
        return num;
    }

    //@PostMapping("login")
    public String loginTest(String username, String password, Model model, HttpSession session) {
        System.out.println(username);
        System.out.println(password);
        String ret = null;
        if (username.isEmpty())
            model.addAttribute("msg", "登录请输入用户名！");
        else {
            session.setAttribute("loginUser", username);
            return "test1";
        }
        return "index";
    }

    @GetMapping("main/testpage")
    public String testPageE() {
        return "main/testpage";
    }

    @GetMapping("/modeltest")
    public String modelTest(Model model) {
        model.addAttribute("message1", "message1 attributeValue");
        return "modeltest";
    }
}
